package com.project.filter;

import com.project.bean.RootBean;
import com.project.bean.UserBean;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 客户管理过滤器
 */
public class CustomerrFilter implements Filter {
    private String root;
    private String location;

    public CustomerrFilter(String root, String location){
        this.root=root;
        this.location=location;
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {


        HttpServletRequest request1=(HttpServletRequest)request;
        HttpServletResponse response1=(HttpServletResponse)response;
        //获取用户
        UserBean user=(UserBean) request1.getSession().getAttribute("loginUser");


        if(user == null) {
            response1.sendRedirect(location);
            return;
        }

        List<RootBean> rootList=user.getUserRoot();
        for (RootBean r : rootList) {
            if (root.equals(r.getName())) {
                chain.doFilter(request1, response1);
                return;
            }
        }

        response1.sendRedirect(location);

    }

    @Override
    public String toString() {
        return "CustomerrFilter{" +
                "root='" + root + '\'' +
                ", location='" + location + '\'' +
                '}';
    }
}
